package com.hainatong.shiphelper.module.warehouse.fragment;

import android.os.Bundle;
import android.view.View;
import cn.bertsir.zbar.Qr.ScanResult;
import cn.bertsir.zbar.QrManager;
import com.blankj.utilcode.util.LogUtils;
import com.hainatong.shiphelper.App;
import com.hainatong.shiphelper.Constants;
import com.hainatong.shiphelper.MvvmLazyFragment;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.FragmentChukuAddListBinding;
import com.hainatong.shiphelper.mainhome.ComFragmentActivity;
import com.hainatong.shiphelper.module.warehouse.adapter.ItemChukuAdapter;
import com.hainatong.shiphelper.module.warehouse.entity.ChukuGoodsParam;
import com.hainatong.shiphelper.module.warehouse.viewmodel.RuKuViewModel;
import com.hainatong.shiphelper.utils.DialogUitl;
import com.hainatong.shiphelper.utils.RecyclerViewHelper;
import com.hainatong.mvvmbase.rxbus.RxBusBaseMessage;
import com.jeremyliao.liveeventbus.LiveEventBus;


public class ChukuAddFragment extends MvvmLazyFragment<FragmentChukuAddListBinding, RuKuViewModel>
{

    private static String CHUKU_ADD_TAG = "ChukuAddFragment";

    private String goodsCode ;

    private ItemChukuAdapter adapter;

    public static ChukuAddFragment newInstance(String scanCode)
    {
        Bundle bundle = new Bundle();
        bundle.putString( CHUKU_ADD_TAG , scanCode );
        ChukuAddFragment addFragment = new ChukuAddFragment();
        addFragment.setArguments( bundle );
        return addFragment;
    }

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_chuku_add_list;
    }

    @Override
    public int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void initView(View view)
    {
        Bundle args = getArguments();
        if(  args != null )
        {
            goodsCode = args.getString(CHUKU_ADD_TAG , "");
        }

        adapter = new ItemChukuAdapter();
        RecyclerViewHelper.addLinerLayoutMgrNoDecoration( viewDataBinding.recyclerview );
        viewDataBinding.recyclerview.setAdapter( adapter );

        adapter.addChildClickViewIds( R.id.btn_more );
        adapter.setOnItemChildClickListener( (adapter1, view1, position) -> {

            switch ( view1.getId() )
            {
                case R.id.btn_more:

                    DialogUitl.showSimpleDialog(getActivity(),"确定要删除？", (dialog, content) -> {
                        // 移除
                        adapter.removeAt( position );
                        setTotalSize( adapter.getData().size() );
                    } );

                    break;
            }

        } );

        // 跳转 选择领用人
        viewDataBinding.tvOk.setOnClickListener( v -> {

            // 把所有 添加的 出库 商品 放到 领用人 页面
            LiveEventBus.get( LinYongFragment.CHUKU_GOODS_PARAMS , RxBusBaseMessage.class)
                        .post( new RxBusBaseMessage(LinYongFragment.CHUKU_PARAMS_CODE , adapter.getData() ) );

            Bundle bundle = new Bundle();
            bundle.putString( Constants.FRAGMNET_TYPE, Constants.VIEW_TYPE_LINGYONG_INFO);
            ComFragmentActivity.start( getActivity(),bundle );

        } );

        //扫描 添加 出库商品
        viewDataBinding.fabAdd.setOnClickListener( v -> {
            startScanQrcode(3);
        } );

        LiveEventBus.get( ChukuGoodsFragment.FINISH_ADD_GOODS, ChukuGoodsParam.class )
                    .observe( this , goodsParam ->{
                        //添加 选择的 物资 信息
                        adapter.addData( goodsParam );
                        adapter.notifyDataSetChanged();
                        setTotalSize( adapter.getData().size() );
                    } );

        LiveEventBus.get(LinYongFragment.FINISH_CONFIRM_CHUKU ,RxBusBaseMessage.class )
                    .observe(this , rxMsg -> {
                        // 切换 显示 出库 成功页面
                        ((ComFragmentActivity)getActivity()).showCommonResult( rxMsg );
                    });

    }

    @Override
    protected void lazyLoad()
    {
       // 不需要请求
        /*
       viewModel.getChukuImpaResult().observe(this , result -> {
           if( result.isSuccess() )
           {
               //(RukuGoodsInfo) result.getData()
           }
           else
           {
               ToastUtils.showShort( result.getMsg() );
           }
       } );
       */

    }

    @Override
    protected void onRetryBtnClick()
    {
    }

    private void setTotalSize(int size)
    {
        String totalCount = String.format( getString( R.string.has_ruku_count ), String.valueOf( size ) );
        viewDataBinding.tvSelectTotal.setText( totalCount );
    }

    public void startScanQrcode( int type )
    {
        QrManager.getInstance().init( App.getScanQrConfigByType( type ) ).startScan(getActivity(), new QrManager.OnScanResultCallback() {
            @Override
            public void onScanSuccess(ScanResult result)
            {
                LogUtils.e(TAG, "onScanSuccess: " + result.getContent());
                if( result.getType() == ScanResult.CODE_QR)
                {
                    //扫描 获取商品 条码  code
                    //ToastUtils.showShort( result.getContent() );
                    //ImpaShelfCodeBean impaCode = new ImpaShelfCodeBean("",result.getContent());
                    //查 商品
                    ComFragmentActivity.startViewByParam(getActivity(),Constants.VIEW_TYPE_CHUKU_GOODS,result.getContent());

                }
            }
        });
    }

}
